/**
 *  *2010-2-28 上午10:43:02  add 
 */
package com.base.myproject.client.busi.storeslist;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Administrator
 *2010-2-28
 * javazgw@gmail.com
 */
public class StoreQuery extends ContentPanel{
	BaseStore<BusiObject> store;
	 TextField<String> cgicode = new TextField<String>();
	 TextField<String> cgcode= new TextField<String>();
	 TextField<String> gcode = new TextField<String>();
	 TextField<String> modeltype= new TextField<String>();
	public StoreQuery()
	{
		init();
	}
	void init() {
		setWidth("760px");
		setHeading("库存余额查询");
		initstore();
		
		initWidget();
	}
	void initstore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		// type.addField("b2cgcode");
		type.addField("inqty");

		type.addField("outqty");
		type.addField("lastqty");
		type.addField("cgicode");
		type.addField("cgcode");
		type.addField("gcode");
		type.addField("qtyunit");
		type.addField("modeltype");
		

		StoreInfo info = new StoreInfo();

		
		info.setTablename("sleeping");
		info.setNeedAllRow("no");
		info.setForcenolimit("true");
		 info.setSql(initreloadparam());
		
		store = new BaseStore<BusiObject>(type, info,false);

	}
	
	void initWidget() {
		
		VerticalPanel vpanel = new VerticalPanel();
		
		vpanel.add(new Html("<br>"));
		vpanel.add(initQuery());
		vpanel.add(new Html("<br>"));
		vpanel.add(CreateGrid());
		
		add(vpanel);
		
	}
	Widget CreateGrid()
	{
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
	
		columns.add(new ColumnConfig("cgicode", "barcode", 100));
		columns.add(new ColumnConfig("cgcode", "名称", 100));
		columns.add(new ColumnConfig("modeltype", "规格型号", 100));
		columns.add(new ColumnConfig("gcode", "类型", 100));

		columns.add(new ColumnConfig("inqty", "总入库数量", 100));
		
		columns.add(new ColumnConfig("outqty", "总出库数量", 100));
		columns.add(new ColumnConfig("lastqty", "库存余额", 100));
		columns.add(new ColumnConfig("qtyunit", "单位", 50));
		
//		
//		columns.add(new ColumnConfig("s_edu", "教育程度", 100));
//		columns.add(new ColumnConfig("s_birid", "生日", 100));
		
//		columns.add(new ColumnConfig("s_idno", "身份证", 50));
//		columns.add(new ColumnConfig("s_mark", "备注", 50));
		
		
		ColumnModel cm = new ColumnModel(columns);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		BaseGrid<BusiObject>  grid = new BaseGrid<BusiObject>(store, cm,false,false,null);
		grid.setSelectionModel(cbs);
//		grid.setAutoHeight(true);
//		grid.setAutoWidth(true);
		grid.setSize(760,300);
		grid.getView().setForceFit(true);
		return grid;
	}
	
	Widget initQuery()
	{
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.add(new Label("barcode："));
		hpanel.add(cgicode);
		hpanel.add(new Label("商品名："));
		hpanel.add(cgcode);
		hpanel.add(new Label("规格型号："));
		hpanel.add(modeltype);
		hpanel.add(new Label("商品类型："));
		hpanel.add(gcode);
		
		Button query = new Button("查询");
		hpanel.add(query);
		query.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				StoreInfo info  = store.getStoreinfo();;
				info.setSql(initreloadparam());
				store.setStoreinfo(info);
				store.reload();
			}
			
		});
		return hpanel;
	}
	String initreloadparam()
	{
		String sql = "";
		sql+=" select sum(a.qty) as inqty,sum(b.qty) as outqty, cast(sum(isnull(a.qty,0)-isnull(b.qty,0)) as decimal(8,2)) as lastqty ,a.cgicode,a.cgcode,a.gcode,a.qtyunit,a.modeltype from     ";
		sql+=" (   ";
		sql+=" select sum(qty) as qty,cgicode,cgcode,gcode,qtyunit,modeltype from storeslistg as z1 where inoutflag=2 and (cgicode like '%25"+(cgicode.getValue()==null?"":cgicode.getValue())+"%25' and cgcode like '%25"+(cgcode.getValue()==null?"":cgcode.getValue())+"%25' and modeltype like '%25"+(modeltype.getValue()==null?"":modeltype.getValue())+"%25' and gcode like '%25"+(gcode.getValue()==null?"":gcode.getValue())+"%25')";
		sql+=" group by cgicode,cgcode,gcode,qtyunit,modeltype     ";
		sql+=" ) as a    ";
		sql+=" full join ";
		sql+=" (   ";
		sql+=" select sum(qty) as qty,cgicode,cgcode,gcode,qtyunit,modeltype from storeslistg as z1 where inoutflag=3 and (cgicode like '%25"+(cgicode.getValue()==null?"":cgicode.getValue())+"%25' and cgcode like '%25"+(cgcode.getValue()==null?"":cgcode.getValue())+"%25' and modeltype like '%25"+(modeltype.getValue()==null?"":modeltype.getValue())+"%25' and gcode like '%25"+(gcode.getValue()==null?"":gcode.getValue())+"%25') ";
		sql+=" group by cgicode,cgcode,gcode,qtyunit,modeltype     ";
		sql+=" ) as b    ";
		sql+=" on a.cgicode=b.cgicode and a.cgcode=b.cgcode and a.gcode = b.gcode and a.modeltype= b.modeltype     ";
		sql+=" group by a.cgicode,a.cgcode,a.gcode,a.qtyunit,a.modeltype ";

		return sql;
	}
}
